//
//  AnimationClass.m
//  TasteTapHero
//
//  Created by Shin Stupid on 5/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AnimationClass.h"
#import "tempAnimationClass.h"

@implementation AnimationClass
@synthesize _iTag;
static CCLayer* CharacterInstance;
+(int)getClassIDByClassName: (Class)c
{
	if(c == [BOSS_1 class])
	{
		return 1101;
	}
    else if(c == [Monter_1 class])
	{
		return 1102;
	}
    else if(c == [Monter_2 class])
	{
		return 1103;
	}
    else if(c == [Monter_3 class])
	{
		return 1104;
	}    
    else if(c == [EXPLOSION class])
	{
		return 1105;
	}

	return 0;
}


#pragma mark PROTOTYPE
-(void)setCharacterAttribute
{
	fTimer=2.0f;	
	/// prototype
}

-(id)initWithParent: (CCLayer*)parentLayer withZ:(int)iZ
{
	//fTimer=0.2f;///2.0f;
	[self setCharacterAttribute];
	int tagg = [AnimationClass getClassIDByClassName:[self class]];
	CCSpriteBatchNode* spriteSheet = (CCSpriteBatchNode*)[parentLayer getChildByTag:tagg];
	
	if (spriteSheet == nil) {
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"%@.plist", _textureName]];
		spriteSheet = [CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"%@.%@", _textureName,tagFile]];
		[parentLayer addChild:spriteSheet z:iZ tag:tagg];
		
	}
	else {		
		CCSpriteFrame *myframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"%@1.%@",_textureName,tagFile]];
		if(myframe==nil)
		{
			return nil;
			
		}
	}
	/////NSLog(@"_textureName=%@ tagfile=%@",_textureName,tagFile);
	
	if ((self = [super initWithSpriteFrameName:[NSString stringWithFormat:@"%@1.%@",_textureName,tagFile]])) {
		
		[spriteSheet addChild:self];
	}
	CharacterInstance=parentLayer;	
	return self;
}


#pragma mark CREATE_ANIMATION
-(CCAnimation *)createAnimationFrom:(int)iFrom To:(int)iTo andDeylayTime:(float)fDelayTime andNumberFrame:(int)iFrame
{
	
	NSMutableArray *animFramesTurnback = [NSMutableArray array];
	for (int i = iFrom; i < iTo; i++) {
		[animFramesTurnback addObject:
		 [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
		  [NSString stringWithFormat:@"%@%d.%@", _textureName, i,tagFile]]];
	}	
	//CCAnimation *animTurnback = [CCAnimation animationWithFrames:animFramesTurnback delay:fDelayTime/iFrame];
    CCAnimation *animTurnback=[CCAnimation animationWithSpriteFrames:animFramesTurnback delay:fDelayTime/iFrame];
   	return animTurnback;
	
}



-(id)createAnimation
{
	//fTimer=2.0f;
	NSMutableArray *animFramesTurnback = [NSMutableArray array];
	for (int i = 0; i <nFrame; i++) {
		[animFramesTurnback addObject:
		 [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
		  [NSString stringWithFormat:@"%@%d.%@", _textureName, i,tagFile]]];
	}	
    CCAnimation *animTurnback=[CCAnimation animationWithSpriteFrames:animFramesTurnback delay:fTimer/nFrame];		
	id action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animTurnback]] ;
	
	return action;	
	
}
-(id)createAnimationOneTime
{
    NSMutableArray *animFramesTurnback = [NSMutableArray array];
	for (int i = 0; i <nFrame; i++) {
		[animFramesTurnback addObject:
		 [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
		  [NSString stringWithFormat:@"%@%d.%@", _textureName, i,tagFile]]];
	}	
    CCAnimation *animTurnback=[CCAnimation animationWithSpriteFrames:animFramesTurnback delay:fTimer/nFrame];		
	//id action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animTurnback]] ;
	id action=[CCAnimate actionWithAnimation:animTurnback];
	return action;
}



-(void)dealloc
{
	[super dealloc];
}
@end
